home *** CD-ROM | disk | FTP | other *** search
- /*
- Printing.c
-
- This file contains the printing routines for "all shapes".
-
- Created 9/2/92 - Dave Hersey
-
- ----------------------------------------------
-
- 2/93 debugged and plopped on GX CD. - dmh
- 9/93 updated to run with the ß2 "GXified" interface files. - PLA
- 12/93 updated for b3. - dmh
- 3/94 updated for b4. - dmh
- 3/94 general cleanup/debugging. - dmh
- 5/94 …and some more. - dmh
- 8/94 universalized. - dmh
- */
-
- #include <stdio.h>
- #include <font library.h>
- #include <GXExceptions.h>
- #include <graphics libraries.h>
- #include <graphics toolbox.h>
- #include <graphics debugging.h>
- #include <qd library.h>
- #include <PrintingManager.h>
- #include <PrintingResTypes.h>
- #include <PrintingMessages.h>
- #include <Collections.h>
-
- #include "Banana Jr.h"
-
-
- /*------ SetUpEditMenuRec ------------------------------------------------------------------------------------*/
- //
- // This routine sets up an gxEditMenuRecord which references our edit menu. This structure
- // is used by the GXJobDefaultFormatDialog and GXJobPrintDialog calls to allow cut, copy,
- // & paste operations from the dialogs.
- //
- void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
- {
-
- edMenuRec->editMenuID = mEdit;
- edMenuRec->cutItem = iCut;
- edMenuRec->copyItem = iCopy;
- edMenuRec->pasteItem = iPaste;
- edMenuRec->clearItem = iClear;
- edMenuRec->undoItem = iUndo;
- }
-
-
- /*------ DoDocFormat ---------------------------------------------------------------------------------*/
- //
- // This routine performs GX's equivalent of the Page Setup (PrStlDialog) call of
- // the old printing architecture.
- //
- OSErr DoDocFormat(WindowPtr wind, gxDialogResult *result)
- {
- OSErr err;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
- TH_Doc doc;
-
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the job
- // format dialog. Remember to check for errors.
-
- if (wind)
- {
- doc = GetDoc(wind);
- docJob = GetDocJob(doc);
- SetUpEditMenuRec(&edMenuRec);
-
- *result = GXJobDefaultFormatDialog(docJob, &edMenuRec);
- err = GXGetJobError(docJob);
- }
-
- return err;
- }
-
-
- /*------ DoPageFormat ------------------------------------------------------------------------------*/
- //
- // This routine performs a by-page format. In other words, it sets formatting info
- // for only the current page.
- //
- OSErr DoPageFormat(WindowPtr wind, gxDialogResult *result)
- {
- OSErr err = noErr;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
- TH_Doc doc;
- TH_Page docPage;
- gxFormat pageFormat;
-
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the job
- // format dialog. Remember to check for errors.
-
- if (wind)
- {
- SetUpEditMenuRec(&edMenuRec);
- doc = GetDoc(wind);
- docJob = GetDocJob(doc);
- docPage = GetDocPage(doc, (*doc)->curPage);
- pageFormat = (*docPage)->pageFormat;
-
- *result = GXFormatDialog(pageFormat, &edMenuRec, nil);
-
- if (*result == gxRevertSelected)
- {
- GXDisposeFormat(pageFormat);
- (*docPage)->pageFormat = GXNewFormat(docJob);
- }
-
- err = GXGetJobError(docJob);
- }
-
- return err;
- }
-
-
- /*------ DoPrinting ----------------------------------------------------------------------------------*/
- //
- // This routine performs GX's equivalent of the PrJobDialog call in the old
- // printing architecture, and then prints the document if the user wants to.
- //
- OSErr DoPrinting(WindowPtr wind)
- {
- OSErr err = noErr;
- gxDialogResult result;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
- TH_Doc doc;
- gxFormat jobFormat;
- gxMapping formatMapping;
- gxViewPort formatVPort;
-
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the print
- // job dialog. Remember to check for errors. If no errors occur, and the user clicks
- // ok, print the window's document.
-
- if (wind)
- {
- doc = GetDoc(wind);
- docJob = GetDocJob(doc);
-
- SetUpEditMenuRec(&edMenuRec);
- result = GXJobPrintDialog(docJob, &edMenuRec);
- err = GXGetJobError(docJob);
-
- if ((result == gxOKSelected) && !(err))
- err = DoPrintLoop(wind);
- }
-
- return err;
- }
-
-
- /*******************************************************************
- MyPrintOneCopy sets up our job collection items for printing
- one copy of a document, and then prints the document.
-
- ********************************************************************/
-
- OSErr MyPrintOneCopy(WindowPtr wind)
- {
- OSErr err;
- Collection jobCollection;
- gxCopiesInfo copiesInfo;
- gxFileDestinationInfo destInfo;
- gxPageRangeInfo pageRangeInfo;
- Ptr oldCopiesInfo = nil, oldPageRangeInfo = nil, oldDestInfo = nil;
- long oldCopiesSize, oldPageRangeInfoSize, oldDestInfoSize;
- TH_Doc doc = GetDoc(wind);
-
-
- /* Get the job collection and set it up to print one copy… */
-
- jobCollection = GXGetJobCollection(GetDocJob(doc));
-
-
- /* Set number of copies to 1. */
-
- copiesInfo.copies = 1;
- err = MyReplaceCollectionItem(&copiesInfo, sizeof(gxCopiesInfo),
- gxCopiesTag, gxPrintingTagID,
- jobCollection, &oldCopiesInfo, &oldCopiesSize);
- nrequire(err, ReplaceCopies_error);
-
-
- /* Set page range to "all". */
-
- pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
- pageRangeInfo.minFromPage = 1;
- pageRangeInfo.simpleRange.fromPage = 1;
- pageRangeInfo.maxToPage = (*doc)->numPages;
- pageRangeInfo.simpleRange.toPage = (*doc)->numPages;
- pageRangeInfo.simpleRange.printAll = true;
- err = MyReplaceCollectionItem(&pageRangeInfo, sizeof(gxPageRangeInfo),
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, &oldPageRangeInfo, &oldPageRangeInfoSize);
- nrequire(err, ReplacePageRange_error);
-
-
- /* Set destination to "printer". */
-
- destInfo.toFile = false;
- err = MyReplaceCollectionItem(&destInfo, sizeof(gxFileDestinationInfo),
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, &oldDestInfo, &oldDestInfoSize);
- nrequire(err, ReplaceDestination_error);
-
-
- /* Print one copy of our document. */
-
- err = DoPrintLoop(wind);
-
-
- /* Restore original number of copies, page range, and output
- destination in case anybody uses that info. */
-
- ReplaceCopies_error:
- MyReplaceCollectionItem(oldCopiesInfo, oldCopiesSize,
- gxCopiesTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplacePageRange_error:
- MyReplaceCollectionItem(oldPageRangeInfo, oldPageRangeInfoSize,
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplaceDestination_error:
- MyReplaceCollectionItem(oldDestInfo, oldDestInfoSize,
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
-
- /* Dispose of the pointers that MyReplaceCollectionItem created. */
-
- if (oldCopiesInfo)
- DisposePtr(oldCopiesInfo);
-
- if (oldPageRangeInfo)
- DisposePtr(oldPageRangeInfo);
-
- if (oldDestInfo)
- DisposePtr(oldDestInfo);
-
- return err;
- }
-
-
-
- /*******************************************************************
- MyReplaceCollectionItem is a generic routine that replaces a
- collection item with the passed data. If the oldData parameter
- is not nil, this routine creates a pointer and returns the data
- that's being replaced in it. (If the item doesn't already exist,
- then a copy of the newData is returned instead.)
-
- ********************************************************************/
-
- OSErr MyReplaceCollectionItem(void *newData, long collectSize,
- OSType collectType, long collectID,
- Collection whichCollection,
- Ptr *oldData, long *oldDataSize)
- {
- OSErr err = noErr;
- long index;
-
- /*
- If we're supposed to return the old data, get it.
- If there is no old data, return a copy of the new data.
- */
-
- if (oldData)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- dontWantIndex,
- oldDataSize,
- dontWantAttributes);
-
- if (err)
- {
- *oldDataSize = collectSize;
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- BlockMove(newData, *oldData, collectSize);
- }
- else
- {
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- err = GetCollectionItem(whichCollection,
- collectType,
- collectID,
- dontWantSize,
- *oldData);
- }
- }
-
- nrequire(err, CouldNotSetOldData);
-
-
- // If we're adding a new collection item, do so. Otherwise, get the
- // existing item's index and replace the old collection item.
-
- err = AddCollectionItem(whichCollection,
- collectType,
- collectID,
- collectSize,
- newData);
-
- if (err == collectionItemLockedErr)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- &index,
- dontWantSize,
- dontWantAttributes);
- if (!err)
- err = ReplaceIndexedCollectionItem(whichCollection,
- index,
- collectSize,
- newData);
- }
-
- CouldNotSetOldData:
- return err;
- }
-
-
-
- /*------ DoPrintLoop ----------------------------------------------------------------------------------*/
- //
- // This routine prints the window's document using whatever job and format
- // is currently attached to it. (If the window wwere just created and then the user
- // selected Print One Copy w/o first selecting Page Setup…, the system default job and
- // format are stored with this document.
- //
- OSErr DoPrintLoop(WindowPtr wind)
- {
- OSErr err = noErr;
- gxJob docJob;
- Str255 title;
- TH_Doc doc;
- TH_Page docPage;
- gxFormat pageFormat;
- gxShape pageShape, coverPage;
- long firstPg, lastPg, pg, numPages;
-
- // If we have a non-nil WindowPtr, start the print job, print a page and then finish
- // the job. Remember to check those errors!
-
- if (wind)
- {
- doc = GetDoc(wind);
- docJob = GetDocJob(doc);
- numPages = (*doc)->numPages;
- GetWTitle(wind, title);
-
- GXGetJobPageRange(docJob, &firstPg, &lastPg);
-
- firstPg = (firstPg < 1)? 1: (firstPg > numPages)? numPages: firstPg;
- lastPg = (lastPg < firstPg)? firstPg: (lastPg > numPages)? numPages: lastPg;
- numPages = lastPg - firstPg +1;
-
- GXStartJob(docJob, title, numPages);
- err = GXGetJobError(docJob);
-
- for (pg = firstPg; (!err) && (pg <= lastPg); pg++)
- {
- docPage = GetDocPage(doc, pg);
- pageFormat = (*docPage)->pageFormat;
- pageShape = GXCopyToShape(nil, GetDocShape(doc, pg));
- CheckForIdiots(pageFormat, pageShape);
-
- GXPrintPage(docJob, pg, pageFormat, pageShape);
-
- GXDisposeShape(pageShape);
- err = GXGetJobError(docJob);
- }
-
- GXFinishJob(docJob);
- if (!err) err = GXGetJobError(docJob);
- }
- return err;
- }
-
-
- void CheckForIdiots(gxFormat aFormat, gxShape pageShape)
- {
- OSErr err;
- Collection fmtCollection;
- T_BananaCollection bananaConfig;
- gxShape theText;
- Str255 textStr;
- long numChars;
-
- fmtCollection = GXGetFormatCollection(aFormat);
-
- err = GetCollectionItem(fmtCollection, kBananaCollectionType,
- gxPrintingTagID, nil, &bananaConfig);
-
- if (!err)
- {
- if (bananaConfig.one || bananaConfig.two ||
- bananaConfig.three || bananaConfig.four)
- {
- theText = GXNewShape(gxTextType);
- GXSetShapeTextSize(theText, ff(32));
- SetShapeCommonFont(theText, timesFont);
- GXMoveShapeTo(theText, ff(10), ff(40));
-
- if (bananaConfig.two) // Idiots setting
- {
- numChars = sprintf((Ptr) &textStr[0], "%s", "Idiots enabled.");
- GXSetText(theText, numChars, textStr, nil);
-
- AddToShape(pageShape, theText);
- GXMoveShape(theText, ff(0), ff(40));
- }
-
- if (bananaConfig.three) // Random Printing setting
- {
- numChars = sprintf((Ptr) &textStr[0], "%s", "Random Printing Enabled.");
- GXSetText(theText, numChars, textStr, nil);
-
- AddToShape(pageShape, theText);
- GXMoveShape(theText, ff(0), ff(40));
- }
-
- if (bananaConfig.four) // Page Shuffling setting
- {
- numChars = sprintf((Ptr) &textStr[0], "%s", "Page Shuffling enabled.");
- GXSetText(theText, numChars, textStr, nil);
-
- AddToShape(pageShape, theText);
- GXMoveShape(theText, ff(0), ff(40));
- }
-
- if (bananaConfig.one) // 360° Rotation setting
- {
- numChars = sprintf((Ptr) &textStr[0], "%s", "360° Rotation enabled.");
- GXSetText(theText, numChars, textStr, nil);
-
- AddToShape(pageShape, theText);
- }
-
- GXDisposeShape(theText);
- }
- }
- }
-
-
-
-
-
-
-